/*
 * Copyright (c) 2008-2011, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.gclient;

import java.awt.event.ActionEvent;
import javax.swing.JFrame;

import org.panteleyev.swing.BasicDialog;

import com.google.gdata.data.photos.ExifTags;
import com.google.gdata.util.ParseException;
import java.awt.BorderLayout;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import org.panteleyev.swing.SwingFactory;

public class GPhotoPropertiesDialog extends BasicDialog<JFrame> {
    private JLabel heightValue = new JLabel();
    private JLabel widthValue = new JLabel();
    private JLabel sizeValue = new JLabel();
    private JLabel cameraValue = new JLabel();
    private JLabel cameraMakeValue = new JLabel();
    private JLabel isoValue = new JLabel();

    /** Creates new form GPhotoPropertiesDialog */
    public GPhotoPropertiesDialog(JFrame parent, GPhoto photo) {
        super(parent);
        initComponents();

        if (photo != null) {
            heightValue.setText(photo.getHeight().toString());
            widthValue.setText(photo.getWidth().toString());
            sizeValue.setText(photo.getSize().toString());

            ExifTags tags = photo.getExifTags();
            if (tags != null) {
                cameraValue.setText(tags.getCameraModel());
                cameraMakeValue.setText(tags.getCameraMake());
                try {
                    Integer iso = tags.getIsoEquivalent();
                    if (iso != null) {
                        isoValue.setText(iso.toString());
                    }
                } catch (ParseException e) {
                }
            }
        }

        validate();
        pack();
    }

    private void initComponents() {
        setTitle(L10N.PHOTO_PROPS_DIALOG_TITLE.s());

        getContentPane().setLayout(new BorderLayout());

        JLabel l1, l2, l3;
        JPanel tab1 = SwingFactory.createPanel(BorderFactory.createEmptyBorder(5, 5, 5, 5), SwingFactory.LAYOUT.BOX_Y,
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                l1 = new JLabel(L10N.PHOTO_PROPS_DIALOG_WIDTH_LABEL.s()),
                Box.createHorizontalStrut(3),
                widthValue,
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(3),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                l2 = new JLabel(L10N.PHOTO_PROPS_DIALOG_HEIGHT_LABEL.s()),
                Box.createHorizontalStrut(3),
                heightValue,
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(3),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                l3 = new JLabel(L10N.PHOTO_PROPS_DIALOG_SIZE_LABEL.s()),
                Box.createHorizontalStrut(3),
                sizeValue,
                Box.createHorizontalGlue()
            )
        );
        SwingFactory.setCommonPreferredSize(l1, l2, l3);
        SwingFactory.setAlignmentX(tab1, LEFT_ALIGNMENT);

        JLabel l4, l5, l6;
        JPanel tab2 = SwingFactory.createPanel(BorderFactory.createEmptyBorder(5, 5, 5, 5), SwingFactory.LAYOUT.BOX_Y,
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                l4 = new JLabel(L10N.PHOTO_PROPS_DIALOG_CAMERA_LABEL.s()),
                Box.createHorizontalStrut(3),
                cameraValue,
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(3),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                l5 = new JLabel(L10N.PHOTO_PROPS_DIALOG_VENDOR_LABEL.s()),
                Box.createHorizontalStrut(3),
                cameraMakeValue,
                Box.createHorizontalGlue()
            ),
            Box.createVerticalStrut(3),
            SwingFactory.createPanel(null, SwingFactory.LAYOUT.BOX_X,
                l6 = new JLabel("ISO:"),
                Box.createHorizontalStrut(3),
                isoValue,
                Box.createHorizontalGlue()
            )
        );
        SwingFactory.setCommonPreferredSize(l4, l5, l6);
        SwingFactory.setAlignmentX(tab2, LEFT_ALIGNMENT);

        JTabbedPane tabs = new JTabbedPane();
        tabs.addTab("Basic", tab1);
        tabs.addTab("Exif", tab2);
        getContentPane().add(tabs, BorderLayout.CENTER);

        getContentPane().add(SwingFactory.createPanel(BorderFactory.createEmptyBorder(5, 5, 5, 5), SwingFactory.LAYOUT.FLOW,
            new JButton(new AbstractAction("Close") {
                @Override
                public void actionPerformed(ActionEvent e) {
                    onOk();
                }
            })
        ), BorderLayout.SOUTH);
    }
}
